﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DesignLife
{
    public partial class Services : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable items = new DataTable();
            //420
            //Summary on the left page
            summaryS.InnerHtml = "We don't do a lot but what we do, we do well. If you've got a project and want the help of a professional, please contact us, or request a free quote to get started today.";
            //
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
            con.Open();
            if (Request.QueryString["id"] == null)
            {
                //Item Category
                DataTable list_Categories = new DataTable();
                SqlCommand com_item = new SqlCommand("select CategoryID from Categories where parrent='service' and Status=1", con);
                list_Categories.Load(com_item.ExecuteReader());
                Boolean clone = false;
                foreach (DataRow row_categories in list_Categories.Rows)
                {
                    DataTable tableCurent = new DataTable();
                    SqlCommand com_ = new SqlCommand("select top 1 *, LEFT(Contents, 420) as Preview from Categories C inner join News N on C.CategoryID = N.IDCategory where N.IDCategory='" + row_categories["CategoryID"].ToString() + "' and N.Status ='True' order by NewsID desc", con);
                    tableCurent.Load(com_.ExecuteReader());
                    if (!clone)
                    {
                        items = tableCurent.Clone();
                        clone = true;
                    }
                    foreach (DataRow row in tableCurent.Rows)
                    {
                        items.ImportRow(row);
                    }
                }
            }
            else {
                try {
                    DataTable tb_checkID = new DataTable();
                    int id = Convert.ToInt32(Request.QueryString["id"].ToString());
                    SqlCommand com_checkID = new SqlCommand("select * from Categories where CategoryID='" + id + "' and Parrent='service' and Status=1", con);
                    tb_checkID.Load(com_checkID.ExecuteReader());
                    Response.Write(tb_checkID.Rows.Count);
                    if (tb_checkID.Rows.Count == 1)
                    {
                        SqlCommand com_ = new SqlCommand("select top 5 * from Categories C inner join News N on C.CategoryID = N.IDCategory where N.IDCategory='" + id + "' and N.Status ='True' order by NewsID desc", con);
                        items.Load(com_.ExecuteReader());
                    }
                }
                catch {
                    Response.Redirect("Services.aspx");
                }
            }
            services_right_items.DataSource = items;
            services_right_items.DataBind();
            con.Close();
        }
    }
}